﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Exercitation.Model;
using Yipusoft.Exercitation.BLL;
using System.Drawing;

public partial class Combat_BaseItemNew : System.Web.UI.Page
{
    string id = "55f5139f-489a-487d-8d55-ff6e555c7fda";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            ListPageContral1.SetFirstPage();
            BindItemList(0);
        }
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        lblPrompt.Text = "";
        ItemSkillInfo itemtemp = ItemSkill.GetItemSkill(new Guid(ddlItemID.SelectedValue.ToString()));
        if (itemtemp != null && itemtemp.ItemLevel > 0)
        {
            lblPrompt.Text = "不允许再添加子类别";
            lblPrompt.ForeColor = Color.Red;
            return;
        }

        if (string.IsNullOrEmpty(txtItemName.Text.Trim()) || string.IsNullOrEmpty(txtalisItemName.Text.Trim()))
        {
            lblPrompt.Text = "类别名称不能为空";
            lblPrompt.ForeColor = Color.Red;
            return;
        }

        if (btnOK.Text.Equals("保存"))
        {
            ItemSkillInfo minfo= ItemSkill.GetItemSkill(new Guid(Request["ID"]));
            minfo.ItemSkillName = txtItemName.Text.Trim();
            minfo.AlisSkillName = txtalisItemName.Text.Trim();
            ItemSkill.Update(minfo);
            lblPrompt.Text = "保存成功";
            lblPrompt.ForeColor = Color.Green;
            btnOK.Text = "添加";
            txtItemName.Text = "";
            txtalisItemName.Text = "";
            return;
        }
        ItemSkillInfo item = new ItemSkillInfo();
        item.ID = Guid.NewGuid();
        item.ParentID = new Guid(ddlItemID.SelectedValue.ToString());
        item.SkillID = Guid.Empty;
        item.ItemSkillName = txtItemName.Text;
        item.AlisSkillName = txtalisItemName.Text;
        item.ItemLevel = itemtemp == null ? 0 : itemtemp.ItemLevel + 1;
        ItemSkill.Insert(item);
        Bind();
        lblPrompt.Text = "添加成功";
        lblPrompt.ForeColor = Color.Green;
        txtItemName.Text = "";
        txtalisItemName.Text = "";
        ListPageContral1.SetFirstPage();
        BindItemList(0);
    }

    private void Bind()
    {
        ddlItemID.Items.Clear();
        List<ItemSkillInfo> skillList = ItemSkill.GetList(" ParentID='" + id + "' OR ParentID IN(Select ID From ItemSkill Where ParentID='" + id + "')");
        List<ItemSkillInfo> skillListpt = skillList.Where(o => o.ParentID == new Guid(id)).ToList<ItemSkillInfo>();
        foreach (ItemSkillInfo info in skillListpt)
        {
            ddlItemID.Items.Add(new ListItem(info.ItemSkillName, info.ID.ToString()));
            List<ItemSkillInfo> skillListchild = skillList.Where(o => o.ParentID == info.ID).ToList<ItemSkillInfo>();
            foreach (ItemSkillInfo iminfo in skillListchild)
            {
                ddlItemID.Items.Add(new ListItem("".PadLeft(2 * iminfo.ItemLevel, '　') + iminfo.ItemSkillName + "[" + iminfo.AlisSkillName + "]", iminfo.ID.ToString()));
            }
        }

        ddlItemID.Items.Insert(0, new ListItem("顶级分类", id));

        if (!string.IsNullOrEmpty(Request["ID"]))
        {
            ItemSkillInfo info= ItemSkill.GetItemSkill(new Guid(Request["ID"].ToString()));
            txtItemName.Text = info.ItemSkillName;
            txtalisItemName.Text = info.AlisSkillName;
            ddlItemID.SelectedValue = info.ParentID.ToString();
            btnOK.Text = "保存";
        }
        
    }

    public void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindItemList(PageIndex);
    }
    private void BindItemList(int pageIndex)
    {
        int count = ItemSkill.GetCount(" ParentID='"+id+"'");
        ListPageContral1.ItemCount = count;
        gvItemList.DataSource= ItemSkill.GetList("ID","Desc"," ParentID='" + id + "'",ListPageContral1.PageSize,pageIndex);
        gvItemList.DataBind();

    }
    protected void gvItemList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = ((ListPageContral1.CurrentPageIndex * ListPageContral1.PageSize) + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void lnbtnDelete_Command(object sender, CommandEventArgs e)
    {
        if (ItemSkill.GetCount(" ParentID='" + e.CommandArgument.ToString() + "'") <= 0)
        {
            ItemSkill.Delete(new Guid(e.CommandArgument.ToString()));
            ListPageContral1.SetFirstPage();
            BindItemList(0);
        }
        else
        {
            Response.Write("<script>alert('此分类下有子分类，请先删除子分类');history.back(0);</script>");
            return;
        }
    }
    protected void lnbtnModify_Command(object sender, CommandEventArgs e)
    {
        Response.Redirect("BaseItemNew.aspx?ID="+e.CommandArgument);
    }
}
